package com.itheima.controller;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;


/*
controller -> 接口 -> 实现类 -> Dao接口 ->Mapper(dao.xml)
*/
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;


    @RequestMapping("/add")
    public Result add (@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.add(permissionIds,menuIds,role);
            return new Result(true, "新增角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增角色失败");
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage (@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }



    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(@RequestParam int id){
        try {
            List<Integer> list = roleService.findMenuIdsByRoleId(id);
            return new Result(true, "查询菜单成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询菜单失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds,Integer[] menuIds,@RequestBody Role role){
        try {
            roleService.edit(permissionIds,menuIds,role);
            return new Result(true,"修改角色成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改角色失败");
        }
    }


    //通过Id查询角色
    @RequestMapping("/findById")
    public Result findById (Integer id) {
        try {
            Role role=roleService.findById(id);
            return new Result(true, "查询角色成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }

    //删除角色
    @RequestMapping("/delete")
    public Result delete (Integer id) {
        try {
            roleService.deleteById(id);
            return new Result(true, "删除角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色失败");
        }
    }

    //查询所有角色
    @RequestMapping("/findAll")
    public Result findAll () {
        try {
            List<Role> list=roleService.findAll();
            return new Result(true, "查询所有角色成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询所有角色失败");
        }
    }

    //根据角色ID查询
    @RequestMapping("/findPermissionsById")
    public Result findPermissionsById (Integer id) {
        try {
            List<Integer> permissionIdsByRoleId = roleService.findPermissionIdsByRoleId(id);
            return new Result(true, "查询权限成功",permissionIdsByRoleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限失败");
        }
    }

}
